package com.gxwzxy.medic.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gxwzxy.medic.entity.Medical;
import com.gxwzxy.medic.entity.otherentity.S201;
import com.gxwzxy.medic.service.MedicalService;
import com.gxwzxy.medic.service.Impl.MedicalServiceImpl;
import com.gxwzxy.medic.until.PageBean;
import com.my.web.servlet.RequestBeanUtils;

/**
 * Servlet implementation class MedicalServlet
 */
public class MedicalServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
       
	/**
	 * 医疗机构分页
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
    public void findMedical(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException {
    	//获取响应数据
    	String sindex = request.getParameter("index");
    	int index = 1;
		try {
    		index = Integer.parseInt(sindex);
    	}catch(NumberFormatException n) {
    		n.printStackTrace();
    	}
		//分页
    	PageBean<Medical> pu = new PageBean<Medical>();
    	pu.setIndex(index);//设置展示的当前页数
    	MedicalService is = new MedicalServiceImpl();
    	is.pageMedical(pu);
		request.setAttribute("pu", pu);
		request.getRequestDispatcher("page/medical/medical.jsp").forward(request, response);
    }

    /**
     * 查询s201关系表
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void findS201(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
    	//处理请求
    	MedicalService ms = new MedicalServiceImpl();
    	//获取s201关系表
    	List<S201> ls = ms.findS201();
    	//获取当前日期
    	Date date=new Date();
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		String d = format.format(date);
		request.setAttribute("ls", ls);
		request.setAttribute("d", d);
		request.getRequestDispatcher("page/medical/addMedical.jsp").forward(request, response);
    }
    
    /**
     * 添加医疗机构
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void saveMedical(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
    	//获取响应数据
    	Medical m = RequestBeanUtils.requestToBean(request, Medical.class);
    	//处理响应请求
    	MedicalService ms = new MedicalServiceImpl();
    	if(ms.saveMedical(m)) {
    		request.setAttribute("url", "MedicalServlet?method=findMedical&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
    	}
    }
    
    /**
     * 根据medicalcode查询医疗机构
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void findMedicalByMcode(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
    	//获取响应数据
    	String medicalcode = request.getParameter("medicalcode");
    	//处理响应请求
    	MedicalService ms = new MedicalServiceImpl();
    	//获取医疗机构信息
    	List<Medical> lm = ms.findMedicalByMcode(medicalcode);
    	//获取关联关系表信息
    	List<S201> ls = ms.findS201();
    	request.setAttribute("lm", lm);
    	request.setAttribute("ls", ls);
    	request.getRequestDispatcher("page/medical/updateMedical.jsp").forward(request, response);
    }
    
    /**
     * 根据medicalcode修改医疗机构
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void updateMedical(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
    	//获取响应数据
    	Medical m = RequestBeanUtils.requestToBean(request, Medical.class);
    	//处理响应请求
    	MedicalService ms = new MedicalServiceImpl();
    	if(ms.updateMedical(m)) {
    		request.setAttribute("url", "MedicalServlet?method=findMedical&&index=1");
			request.getRequestDispatcher("tips.jsp").forward(request, response);
    	}
    }
    
    /**
     * 根据medicalcode删除医疗机构
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    public void deleteMedical(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException{
    	//获取响应数据
    	String medicalcode = request.getParameter("medicalcode");
    	//处理响应请求
    	MedicalService ms = new MedicalServiceImpl();
    	if(ms.deleteMedical(medicalcode)) {
    		response.getWriter().write("true");
    	}
    }
}
